package com.lx.lxsql.execute;    //说明: 解析并执行脚本

import com.lx.util.LX;
import com.lx.util.LXLog;
import com.lx.lxsql.entity.SIType;
import com.lx.lxsql.entity.StatementInformation;

import java.util.*;

/**
 * { ylx } 2021/9/12 21:16
 */
public class ExecuteUtil {
    private LinkedList<StatementInformation> sis;

    public ExecuteUtil(LinkedList<StatementInformation> sis) {
        this.sis = sis;
        LXLog.debug(sis.toString());
    }


    public boolean hasNext(){
        return sis.size()>0;
    }
    //说明: 弹出当前节点
    /**{ ylx } 2021/9/20 20:00 */
    public StatementInformation next(){
        LX.exMsg(!hasNext(),"下个节点不存在!");
        LXLog.debug("取出当前节点:"+current());
        return sis.poll();
    }
    //说明: 获取当前节点
    /**{ ylx } 2021/9/20 20:00 */
    public StatementInformation current(){
        LX.exMsg(!hasNext(),"下个节点不存在!");
        return sis.get(0);
    }

    //说明: 判断接下来的是否正确
    /**{ ylx } 2021/9/20 19:46 */
    public List<StatementInformation> exTypeNextList(SIType...types){
        LX.exObj(types, "预期节点列表不能为空!");
        List<StatementInformation> ls = new LinkedList<>();
        for (int i = 0; i < types.length; i++) {
            LXLog.debug("预计节点:"+types[i]+";实际节点:"+current());
            ls.add(current());
            if (!types[i].isType(next())){
                LX.exMsg("当前节点异常:"+types[i]);
            }
        }
        return ls;
    }
    //说明: 判断下一个节点是否在类型中
    /**{ ylx } 2021/9/20 19:46 */
    public StatementInformation exNextInType(SIType...types){
        LX.exObj(types, "语句节点不能为空");
        List<SIType> typels = Arrays.asList(types);
        if (!typels.contains(current().getSiType())){
            LX.exMsg("未获取到预期节点:"+typels);
        }
        return next();
    }

}
